import requests
import redis
from concurrent.futures import ThreadPoolExecutor

# Redis配置
REDIS_HOST = 'localhost'
REDIS_PORT = 6379
REDIS_DB = 0
REDIS_KEY = 'proxies'

# 免费代理网站
PROXY_SOURCES = [
    "https://api.proxyscrape.com/v2/?request=getproxies&protocol=http&timeout=10000&country=all",
    "https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/http.txt",
    "https://raw.githubusercontent.com/clarketm/proxy-list/master/proxy-list-raw.txt"
]

# 初始化Redis连接
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, decode_responses=True)


def fetch_proxies():
    """从代理源抓取代理IP"""
    proxies = set()
    for url in PROXY_SOURCES:
        try:
            response = requests.get(url, timeout=10)
            if response.status_code == 200:
                proxies.update(response.text.splitlines())
        except Exception as e:
            print(f"Error fetching proxies from {url}: {e}")
    return proxies


def validate_proxy(proxy):
    """验证代理IP是否有效"""
    test_url = "http://httpbin.org/ip"  # 测试URL
    proxies = {
        "http": f"http://{proxy}",
        "https": f"http://{proxy}"
    }
    try:
        response = requests.get(test_url, proxies=proxies, timeout=5)
        if response.status_code == 200:
            print(f"Valid proxy: {proxy}")
            return proxy
    except Exception:
        pass
    return None


def update_proxy_pool():
    """更新代理池"""
    print("Fetching proxies...")
    proxies = fetch_proxies()
    print(f"Fetched {len(proxies)} proxies. Validating...")

    valid_proxies = []
    with ThreadPoolExecutor(max_workers=20) as executor:
        results = executor.map(validate_proxy, proxies)
        valid_proxies = [proxy for proxy in results if proxy]

    print(f"Found {len(valid_proxies)} valid proxies.")
    if valid_proxies:
        redis_client.delete(REDIS_KEY)  # 清空旧代理池
        redis_client.sadd(REDIS_KEY, *valid_proxies)  # 添加新代理IP
        print("Proxy pool updated successfully.")
    else:
        print("No valid proxies found.")


def get_random_proxy():
    """从代理池中随机获取一个代理IP"""
    proxy = redis_client.srandmember(REDIS_KEY)
    if proxy:
        return {"http": f"http://{proxy}", "https": f"http://{proxy}"}
    else:
        print("No proxies available in the pool.")


def get_proxy():
    proxy = redis_client.srandmember(REDIS_KEY)
    if proxy:
        return f"https://{proxy}"
    else:
        print("No proxies available in the pool.")
        return None


if __name__ == "__main__":
    # 更新代理池
    update_proxy_pool()

    # 获取随机代理
    proxy = get_random_proxy()
    print(f"Random proxy: {proxy}")
